package example.com.terry.servlet;

import example.com.terry.bean.WorkDto;
import example.com.terry.utils.CookieUtils;
import example.com.terry.utils.JDBCUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * ClassName: QueryWorkServlet <br/>
 * Description: 作业接口查询 <br/>
 * Date: 2020/5/21 下午5:27 <br/>
 * <br/>
 *
 * @author ls
 * <p/>
 * 修改记录
 * @version 1.0<br />
 */
@WebServlet("/queryWorkServlet")
public class QueryWorkServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        if (CookieUtils.isSign(req)) {
            String id = req.getParameter("id");


            if (id != null) {
                String sql1 = "select * from tb_work where  id = " + id;
                ResultSet resultSet = JDBCUtils.initQuery(sql1);

                try {
                    // 作业集合
                    // 循环取数据库出查询出来的每条信息结果
                    WorkDto workDto = new WorkDto();
                    if (resultSet.next()) {
                        //取出当前这条信息的字段信息
                        String sechId = resultSet.getString("id");
                        String scheduleText = resultSet.getString("work_text");
                        String scheduleDate = resultSet.getString("work_date");
                        String type = resultSet.getString("type");

                        workDto.setId(sechId);
                        workDto.setWorkDate(scheduleDate);
                        workDto.setWorkText(scheduleText);
                        workDto.setType("0".equals(type) ? "未完成" : "1".equals(type) ? "已完成" : "完成未批改");
                        workDto.setUser(CookieUtils.getUser(req));
                    }
                    resp.getWriter().println(workDto);
                } catch (Exception e) {

                }
            } else {

                String sql1 = "select * from tb_work where  user_id = '" + CookieUtils.getUser(req) + "'";
                ResultSet resultSet = JDBCUtils.initQuery(sql1);

                try {
                    // 作业集合
                    ArrayList<WorkDto> workDtos = new ArrayList<>();
                    // 循环取数据库出查询出来的每条信息结果
                    while (resultSet.next()) {
                        //取出当前这条信息的字段信息
                        String sechId = resultSet.getString("id");
                        String scheduleText = resultSet.getString("work_text");
                        String scheduleDate = resultSet.getString("work_date");
                        String type = resultSet.getString("type");
                        WorkDto workDto = new WorkDto();
                        workDto.setId(sechId);
                        workDto.setWorkDate(scheduleDate);
                        workDto.setWorkText(scheduleText);
                        workDto.setType("0".equals(type) ? "未完成" : "1".equals(type) ? "已完成" : "完成未批改");
                        workDto.setUser(CookieUtils.getUser(req));
                        workDtos.add(workDto);
                    }
                    resp.getWriter().println(workDtos);
                } catch (Exception e) {

                }

            }
        }
    }
}
